define(['jquery', 'bootstrap', 'frontend', 'form'], function ($, undefined, Frontend, Form) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        index: function () {
            Controller.api.bindevent();
        },

        detail: function () {
            Controller.api.bindevent();
            require(['jstree'], function () {
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#channeltree").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#channeltree").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
                $('#channeltree').on("changed.jstree", function (e, data) {
                    var Params = new Array();
                    //先获取参数数组
                    Params = getUrlParams();
                    //分析参数中是否存在当前元素名的参数

                    if(Params["filter[shopcat_ids]"]===undefined){
                        //不存在目标参数，则直接加上
                        if(location.search) s = "&";
                        else s = "?";
                        location.href = location.href+s+encodeURI("filter[shopcat_ids]="+data.selected);
                    }else{
                        //存在目标参数，则修改后重新拼装
                        Params["filter[shopcat_ids]"] = data.selected;
                        s = toUrlParams(Params);
                        location.href = location.pathname+'?'+ encodeURI(s) ;
                    }
                    return false;
                });
                $('#channeltree').jstree({
                    "themes": {
                        "stripes": true
                    },
                    "checkbox": {
                        "keep_selected_style": false,
                        "three_state": false
                    },
                    "types": {
                        "channel": {
                            "icon": "fa fa-th",
                        },
                        "list": {
                            "icon": "fa fa-list",
                        },
                        "link": {
                            "icon": "fa fa-link",
                        },
                        "disabled": {
                            "check_node": false,
                            "uncheck_node": false
                        }
                    },
                    'plugins': ["types", "checkbox"],
                    "core": {
                        "multiple": false,
                        'check_callback': false,
                        "data": Config.channelList
                    }
                });
            });
        },

        join: function () {
            Controller.api.bindevent();
        },

        joinstep1: function () {
            Controller.api.bindevent();
            $("#c-protocol").trigger("fa.event.agreementprotocol");
        },

        joinstep2: function () {
            Controller.api.bindevent();
        },

        joinstep3: function () {
            //本地验证未通过时提示
            $("#edit-form").data("validator-options", validatoroptions);
            Controller.api.bindevent();
            $("#c-businessArea1").trigger("fa.event.businessAreaupdated",'edit');
        },
        joinstep4: function () {
            //本地验证未通过时提示
            $("#edit-form").data("validator-options", validatoroptions);
            Controller.api.bindevent();
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');
            $("#c-bankAreaIds1").trigger("fa.event.bankAreaIdsupdated",'edit');
        },
        joinstep5: function () {
            //本地验证未通过时提示
            $("#edit-form").data("validator-options", validatoroptions);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                //不可见的元素不验证
                $("form[role=form]").data("validator-options", {ignore: ':hidden'});
                Form.api.bindevent($("form[role=form]"));
                //四级地区联动参数传递
                $("#c-businessArea2").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea1").val()}};
                });
                $("#c-businessArea3").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea2").val()}};
                });
                $("#c-businessArea4").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea3").val()}};
                });

                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: $("#c-category1").val()}};
                });
                $("#c-category3").data("params", function (obj) {
                    return {custom: {pid: $("#c-category2").val()}};
                });
                $("#c-category4").data("params", function (obj) {
                    return {custom: {pid: $("#c-category3").val()}};
                });

                $("#c-bankAreaIds2").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds1").val()}};
                });
                $("#c-bankAreaIds3").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds2").val()}};
                });
                $("#c-bankAreaIds4").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds3").val()}};
                });
                //一级地区选定，清除二、三、四级选项
                $(document).on("change", "#c-businessArea1", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea2').selectPageClear();
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($('#c-businessArea1').val());
                    });
                });
                $(document).on("change", "#c-category1", function () {
                    require(['selectpage'], function () {
                        $('#c-category2').selectPageClear();
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val($('#c-category1').val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds1", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds2').selectPageClear();
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($('#c-bankAreaIds1').val());

                    });
                });
                //二级地区选定，清除三、四级选项
                $(document).on("change", "#c-businessArea2", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val());
                    });
                });
                $(document).on("change", "#c-category2", function () {
                    require(['selectpage'], function () {
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds2", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val());

                    });
                });
                //三级地区选定，清除四级选项
                $(document).on("change", "#c-businessArea3", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val( $("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val());
                    });
                });
                $(document).on("change", "#c-category3", function () {
                    require(['selectpage'], function () {
                        $('#c-category4').selectPageClear();
                        $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds3", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val());
                    });
                });
                //拼接四级地区选项值，用于保存
                $(document).on("change", "#c-category4", function () {
                    $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val()+","+$("#c-businessArea4").val());
                });
                $(document).on("change", "#c-category4", function () {
                    $("#c-areaIds").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val()+","+$("#c-category4").val());
                });
                $(document).on("change", "#c-bankAreaIds4", function () {
                    $("#c-bankAreaIds").val( $("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val()+","+$("#c-bankAreaIds4").val());
                });
                //四级地区默认值设置（只执行一次）
                $(document).one("fa.event.businessAreaupdated", "#c-businessArea1", function () {
                    var a = $("#c-businessAreaPath").val().split(",");
                    $("#c-businessArea1").val(a[0]);
                    $("#c-businessArea2").val(a[1]);
                    $("#c-businessArea3").val(a[2]);
                    $("#c-businessArea4").val(a[3]);
                });
                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                    var a = $("#c-areaIds").val().split(",");
                    $("#c-category1").val(a[0]);
                    $("#c-category2").val(a[1]);
                    $("#c-category3").val(a[2]);
                    $("#c-category4").val(a[3]);
                });
                $(document).one("fa.event.bankAreaIdsupdated", "#c-bankAreaIds1", function () {
                    var a = $("#c-bankAreaIds").val().split(",");
                    $("#c-bankAreaIds1").val(a[0]);
                    $("#c-bankAreaIds2").val(a[1]);
                    $("#c-bankAreaIds3").val(a[2]);
                    $("#c-bankAreaIds4").val(a[3]);
                });
                //设置银行开户名为公司名
                $(document).one("change", "#c-shopCompany", function () {
                    $("#c-bankUserName").val($("#c-shopCompany").val());
                });
                //设置纳税人识别号为营业执照号
                $(document).one("change", "#c-businessLicence", function () {
                    $("#c-taxpayerNo").val($("#c-businessLicence").val());
                });

                //页面加载后，设置未同意协议和下一步按钮禁止属性
                $(document).on("fa.event.agreementprotocol", "#c-protocol", function () {
                    $(this).attr("checked",false);
                    $('.btn-success').attr("disabled",true);
                    $('.msg-box').hide();
                });
                //同意协议，则取消下一步按钮禁止属性
                $(document).on("click", "#c-protocol", function () {
                    if(this.checked){
                        $('.btn-success').removeAttr("disabled");
                        $('.msg-box').hide();
                    }else{
                        $('.btn-success').attr("disabled",true);
                        $('.msg-box').show();
                    }
                });

            }
        }
    };
    return Controller;
});